<?php


namespace app\admin\controller\good;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="商品列表")
 */
class Goodlist extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Goodlist();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $session = session("admin");
            //类型（0超管，1业务员，2仓库员）
            if($session["type"] == 1){
                $count = $this->model
                ->where($where)
                ->where("status",0)
                ->count();
                $list = $this->model
                ->where($where)
                ->where("status",0)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                    //分类
                    $good_type = Db::name('good_type')->where('id',$v['good_type_id'])->field('title')->find();
                    if(!empty($good_type)){
                        $v['good_type_name'] = $good_type['title'];
                    }else{
                        $v['good_type_name'] = "";
                    }
                    
                    $list[$k] = $v;
                }
            }else{
                $count = $this->model
                ->where($where)
                ->count();
                $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                    //分类
                    $good_type = Db::name('good_type')->where('id',$v['good_type_id'])->field('title')->find();
                    if(!empty($good_type)){
                        $v['good_type_name'] = $good_type['title'];
                    }else{
                        $v['good_type_name'] = "";
                    }
                    
                    $v["admin_type"] = $session["type"];
                    $list[$k] = $v;
                }
            }

            // $count = $this->model
            //     ->where($where)
            //     ->where("status",0)
            //     ->count();
            // $list = $this->model
            //     ->where($where)
            //     ->where("status",0)
            //     ->page($page, $limit)
            //     ->order($this->sort)
            //     ->select();
            // foreach ($list as $k => $v){
            //     //分类
            //     $good_type = Db::name('good_type')->where('id',$v['good_type_id'])->field('title')->find();
            //     $v['good_type_name'] = $good_type['title'];
            //     $v["admin_type"] = $session["type"];
            //     $list[$k] = $v;
            // }
          
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $session = session("admin");
        $good_type = Db::name("good_type")->order("id desc")->select()->toArray();
        $this->assign("good_type",$good_type);
        $this->assign("admin_type",$session["type"]);
        return $this->fetch();
    }


    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('商品名称不能为空');
            }
            if(empty($post['good_type_id'])){
                $this->error('分类不能为空');
            }
            if(empty($post['bottom_price'])){
                $this->error('最低价不能为空');
            }
            if(empty($post['stock'])){
                $this->error('库存不能为空');
            }
            if(empty($post['bar_code'])){
                $this->error('商品条码不能为空');
            }
            $flag = Db::name("goods")->where("bar_code",$post["bar_code"])->find();
            if(!empty($flag)){
                $this->error('商品条码已存在，请重新输入');
            }
            $data = [
                'title'=>$post['title'],
                'good_type_id'=>$post['good_type_id'],
                'bottom_price'=>$post['bottom_price'],
                'bar_code'=>$post['bar_code'],
                'stock'=>$post['stock'],
                'create_time'=>time()
            ];
            try {
                $save = $this->model->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $good_type = Db::name("good_type")->order("id desc")->select()->toArray();
        $this->assign("good_type",$good_type);
        return $this->fetch();
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('商品名称不能为空');
            }
            if(empty($post['good_type_id'])){
                $this->error('分类不能为空');
            }
            if(empty($post['bottom_price'])){
                $this->error('最低价不能为空');
            }
            if(empty($post['stock'])){
                $this->error('库存不能为空');
            }
            if(empty($post['bar_code'])){
                $this->error('商品条码不能为空');
            }
            // $flag = Db::name("goods")->where("bar_code",$post["bar_code"])->find();
            // if(!empty($flag)){
            //     $this->error('商品条码已存在，请重新输入');
            // }
            
            $data = [
                'title'=>$post['title'],
                'good_type_id'=>$post['good_type_id'],
                'bottom_price'=>$post['bottom_price'],
              //  'bar_code'=>$post['bar_code'],
                'stock'=>$post['stock']
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
            } catch (\Exception $e) {
                $this->error('保存失败' );
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $good_type = Db::name("good_type")->order("id desc")->select()->toArray();
            $this->assign("good_type",$good_type);
            $this->assign('row', $row);
        }
        return $this->fetch();
    }

    
 
} 